
package com.iot.manage.biz.service.impl;

import java.util.List;
import org.springframework.stereotype.Service;
import com.iot.manage.api.domain.SysMenu;
import com.iot.manage.api.domain.SysRoleMenu;
import com.iot.manage.biz.service.SysRoleService;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

@Service
@RequiredArgsConstructor
public class SysRoleServiceImpl implements SysRoleService {

	@Setter
	private Throwable cause;

	
	public List<SysMenu> getRoleMenuTree(String roleId) {
		List<SysMenu> children = SysMenu.dao.find("SELECT A.*,(CASE  WHEN (SELECT COUNT(MENU_ID) AS COUNT FROM "+SysRoleMenu.TABLE_NAME+" WHERE MENU_ID=A.ID AND ROLE_ID=?)>0 THEN 1 ELSE  0 END )  as checked FROM " + SysMenu.TABLE_NAME + " A WHERE A.PARENTID='-1'   ORDER BY A.SORT ASC ", roleId);
		for (SysMenu menu : children) {
			getOrgChildren(menu, roleId);
		}
		return children;
	}
	
	public void getOrgChildren(SysMenu parent, String roleId) {
		List<SysMenu> children = SysMenu.dao.find("SELECT A.*,(CASE  WHEN (SELECT COUNT(MENU_ID) AS COUNT FROM "+SysRoleMenu.TABLE_NAME+" WHERE MENU_ID=A.ID AND ROLE_ID=?)>0 THEN 1 ELSE  0 END )  as checked FROM " + SysMenu.TABLE_NAME + " A WHERE A.PARENTID=?   ORDER BY A.SORT ASC ", roleId,parent.getId());
		for (SysMenu menu : children) {
			getOrgChildren(menu, roleId);
		}
		parent.setChildren(children);
	}

}
